package com.iot.manager.ui.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.iot.manager.R;
import com.iot.manager.data.models.DeviceData;
import com.iot.manager.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class DeviceDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private static final int ITEMS_PER_PAGE = 20;

    private List<DeviceData> dataList;
    private int currentPage = 0;
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    private OnPageChangeListener pageChangeListener;

    public interface OnPageChangeListener {
        void onPageChanged(int currentPage, int totalPages);
    }

    public DeviceDataAdapter() {
        this.dataList = new ArrayList<>();
    }

    public DeviceDataAdapter(List<DeviceData> dataList) {
        this.dataList = dataList;
    }

    public void setOnPageChangeListener(OnPageChangeListener listener) {
        this.pageChangeListener = listener;
    }

    public void setData(List<DeviceData> dataList) {
        this.dataList = dataList;
        this.currentPage = 0;
        notifyDataSetChanged();
        notifyPageChanged();
    }

    public void updateData(List<DeviceData> newData) {
        this.dataList.clear();
        if (newData != null) {
            this.dataList.addAll(newData);
        }
        this.currentPage = 0;
        notifyDataSetChanged();
        notifyPageChanged();
    }

    private void notifyPageChanged() {
        if (pageChangeListener != null) {
            int totalPages = (int) Math.ceil((double) dataList.size() / ITEMS_PER_PAGE);
            pageChangeListener.onPageChanged(currentPage + 1, totalPages);
        }
    }

    public boolean hasNextPage() {
        return (currentPage + 1) * ITEMS_PER_PAGE < dataList.size();
    }

    public boolean hasPreviousPage() {
        return currentPage > 0;
    }

    public void nextPage() {
        if (hasNextPage()) {
            currentPage++;
            notifyDataSetChanged();
            notifyPageChanged();
        }
    }

    public void previousPage() {
        if (hasPreviousPage()) {
            currentPage--;
            notifyDataSetChanged();
            notifyPageChanged();
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalItems() {
        return dataList.size();
    }

    public int getItemsPerPage() {
        return ITEMS_PER_PAGE;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_HEADER : TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_device_data_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_device_data, parent, false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            // 表头不需要绑定数据
            return;
        }

        ItemViewHolder itemHolder = (ItemViewHolder) holder;
        int dataPosition = position - 1 + (currentPage * ITEMS_PER_PAGE);
        if (dataPosition >= dataList.size()) {
            return;
        }

        DeviceData data = dataList.get(dataPosition);
        if (data == null) {
            return;
        }
        
        // 设置行号
        itemHolder.tvRowNumber.setText(String.valueOf(dataPosition + 1));
        
        // 设置时间
        if (data.getTimestamp() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
            itemHolder.tvTime.setText(sdf.format(data.getTimestamp()));
        } else {
            itemHolder.tvTime.setText("-");
        }
        
        // 设置温度
        if (data.getTemperature() != null) {
            itemHolder.tvTemperature.setText(String.format("%.1f°C", data.getTemperature()));
        } else {
            itemHolder.tvTemperature.setText("-");
        }
        
        // 设置湿度
        if (data.getHumidity() != null) {
            itemHolder.tvHumidity.setText(String.format("%.1f%%", data.getHumidity()));
        } else {
            itemHolder.tvHumidity.setText("-");
        }
    }

    @Override
    public int getItemCount() {
        if (dataList == null || dataList.isEmpty()) {
            return 1; // 只返回表头
        }
        int start = currentPage * ITEMS_PER_PAGE;
        int end = Math.min(start + ITEMS_PER_PAGE, dataList.size());
        return end - start + 1; // +1 for header
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        final TextView tvRowNumber;
        final TextView tvTime;
        final TextView tvTemperature;
        final TextView tvHumidity;

        ItemViewHolder(View itemView) {
            super(itemView);
            tvRowNumber = itemView.findViewById(R.id.tvRowNumber);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvTemperature = itemView.findViewById(R.id.tvTemperature);
            tvHumidity = itemView.findViewById(R.id.tvHumidity);
        }
    }
} 